package com.pandora.framework.vo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.pandora.common.aop.Interceptor;
import com.pandora.common.aop.InvocationInfo;
import com.pandora.common.db.DBSession;
import com.pandora.common.db.DBSessionHolder;
import com.pandora.common.exception.EduException;

//TODO 事务业务基类
public class BaseMultiBean implements Interceptor {

	protected DBSession dbSession = null;
	protected Connection connection = null;
	protected PreparedStatement pstmt = null;
	private ResultSet rset = null;
	protected String sql = "";
	protected ArrayList<Object> fieldList = null;
	protected Map<String, Object> conditons = null;

	public BaseMultiBean() {
		dbSession = null;
		connection = null;
		pstmt = null;
		rset = null;
		sql = "";
		fieldList = new ArrayList<Object>();
		conditons = new HashMap<String, Object>();
	}

	public void before(InvocationInfo paramInvocationInfo) throws EduException {
		DBSession dbSession = new DBSession();
		DBSessionHolder.setCurrendDBSession(dbSession);
	}

	public void after(InvocationInfo paramInvocationInfo) throws EduException {
		DBSession dbSession = DBSessionHolder.getCurrentDBSession();
		if (dbSession != null) {
			dbSession.commit();
		}
	}

	public void last(InvocationInfo paramInvocationInfo) throws EduException {

		conditons.clear();
		conditons = null;
		fieldList.clear();
		fieldList = null;
		sql = "";
		DBSession.closeResultSet(rset);
		DBSession.closePreparedStatement(pstmt);
		DBSession.closeConnection(connection);

		DBSession dbSession = DBSessionHolder.getCurrentDBSession();
		if (dbSession != null) {
			dbSession.closeConnection();
		}
		DBSessionHolder.clearDBSession();
	}

	public void exceptionThrow(InvocationInfo paramInvocationInfo) throws EduException {
		DBSession dbSession = DBSessionHolder.getCurrentDBSession();
		if (dbSession != null) {
			dbSession.rollback();
		}
	}

	protected void getDB() {
		dbSession = DBSessionHolder.getCurrentDBSession();
		connection = dbSession.getConnection();
	}
}